package com.example.administrator.yanfoxconn.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.bean.FileName;
import com.example.administrator.yanfoxconn.bean.YJExceMessage;
import com.example.administrator.yanfoxconn.widget.TouchImageView;

import java.util.List;

/**
 * @Description 營建整改 流程列表
 * @Author song
 * @Date 7/26/21 9:41 AM
 */
public class YJExceZGRecordAdapter extends BaseAdapter {
    private List<YJExceMessage> lists;
    public Context mContext;
    private OnClickListener onClickListener;

    public YJExceZGRecordAdapter(Context context, List<YJExceMessage> lists) {

        this.mContext = context;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        Log.e("--------","lists.size()==="+lists.size());
        return lists.size();
    }

    @Override
    public Object getItem(int i) {
        return lists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        int num = position +1;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_yj_exce_record, null);

            holder.tvNum = convertView.findViewById(R.id.tv_num);
            holder.tvContent = convertView.findViewById(R.id.tv_content);
            holder.tvName = convertView.findViewById(R.id.tv_name);
            holder.tvDate = convertView.findViewById(R.id.tv_date);
            holder.ll110 = convertView.findViewById(R.id.ll_get_image);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvNum.setText(num+"");
        holder.tvContent.setText(lists.get(position).getTreat_desp());
        holder.tvDate.setText(lists.get(position).getTreat_date());
        holder.tvName.setText(lists.get(position).getTreat_people_name());
        holder.ll110.removeAllViews();
        if(lists.get(position).getExce_statue().equals("1")){
            holder.tvNum.setBackgroundColor( mContext.getResources().getColor(R.color.color_00CCFF));
        }else if(lists.get(position).getExce_statue().equals("2")){
            holder.tvNum.setBackgroundColor(mContext.getResources().getColor(R.color.color_42D42B));}
        List<FileName> photos = lists.get(position).getPhoto();

        for (int i=0;i<photos.size();i++) {

            TouchImageView imageView = new TouchImageView(mContext);
//                        ImageView imageView = new ImageView(ZhiyinshuiExceDetailActivity.this);
            String imgStr = photos.get(i).getEXCE_FILENAME1();
            Log.e("------imgStr-----", "======" + imgStr);
            byte[] decode = Base64.decode(imgStr, Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
            imageView.setAdjustViewBounds(true);
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageView.setImageBitmap(bitmap);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 200);
            layoutParams.setMargins(10, 10, 10, 10);
            imageView.setLayoutParams(layoutParams);
            int finalI = i;
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onClickListener.onclick(position, finalI, view);
                }
            });


            holder.ll110.addView(imageView);
        }


        return convertView;
    }

    private class ViewHolder {
        public GridLayout ll110;
        public TextView tvContent;
        public TextView tvCpc;
        public TextView tvDate,tvName;
        public TextView tvNum;
        public TextView tvPhone;
        public TextView tvPosition;
    }

    public void setOnClickListener(OnClickListener paramOnClickListener)
    {
        this.onClickListener = paramOnClickListener;
    }

    public  interface OnClickListener
    {
        void onclick(int paramInt1, int paramInt2, View paramView);
    }
}
